/*User program on Nachos
  For project 4
*/

/*Author: Yuzhe Wu 4/14*/

#include "create.h"

void main() {
	int myNurseId = -1;
	int i = 0;
	struct examSheet xraySheet;
	struct examSheet myLocalExamSheet;
	int sympton = -1;
	int id = -1;
	bool debugFlag = true;

	/*Printf("P4DEBUG: I am xray, in!\n");*/
	doCreate();
	
	rpcAcquireLock(xrayIndexLock);
	id=rpcGetValueMV(xrayIndex,0);
	rpcSetValueMV(xrayIndex,0,(id+1));
	rpcReleaseLock(xrayIndexLock);
	
	if(debugFlag == true) {
		Printf("myDEBUG: doctor [%d]: I entered!\n", id);
	}
	
	rpcAcquireLock(xrayRoomLock[id]);
	
	while(1) {
		rpcSetValueMV(xrayState,id,FREE);
		if(debugFlag == true) {
			Printf("myDEBUG:XRAY MAN PREPARE TO WAIT!!!\n");
		}
		rpcWaitCV(xrayRoomLock[id],xrayPatientCV[id]);
		
		if(debugFlag == true) {
			Printf("myDEBUG:XRAY MAN WAKEN UP BY PATIENT\n");
		}	
		rpcGetExamSheet(xrayExamSheet[id],&xraySheet);
		
		/*
		interacion between patient and XRay techinician
		following sequence of signal and printf changed by Tang
		to synchronize with patient
		*/
		
		Printf("Xray technician [%d] asks Adult Patient [%d] to get on the table.\n",id,xraySheet.patientID);
		rpcSignalCV(xrayRoomLock[id],xrayPatientCV[id]);
		
		rpcWaitCV(xrayRoomLock[id],xrayPatientCV[id]);
		Printf("Xray Technician [%d] takes an Xray Image of Adult Patient [%d].\n",id,xraySheet.patientID);
		
		if((xraySheet.imageNum)>1) {
			for(i=0;i<xraySheet.imageNum-1;i++) {
				rpcSignalCV(xrayRoomLock[id],xrayPatientCV[id]);
				Printf("Xray Technician [%d] asks Adult Patient [%d] to move.\n",id,xraySheet.patientID);
				rpcWaitCV(xrayRoomLock[id],xrayPatientCV[id]);
				Printf("Xray Technician [%d] takes another Xray Image of Adult Patient [%d].\n",id,xraySheet.patientID);		
			}
		}
		
		
		/*record the result to the examination sheet*/
		sympton = Rand(3);
		if(sympton ==0) {
			xraySheet.xrayResult=NOTHING;
		}
		else if(sympton == 1) {
			xraySheet.xrayResult = BREAK;
		}
		else{
			xraySheet.xrayResult = COMPOUND;
		}
		
		if(xraySheet.xrayResult == NOTHING){
			Printf("Xray Technician [%d] records nothing on Adult Patient [%d]'s examination sheet.\n",id,xraySheet.patientID);
		}
		else if(xraySheet.xrayResult == BREAK){
			Printf("Xray Technician [%d] records break on Adult Patient [%d]'s examination sheet.\n",id,xraySheet.patientID);
		}
		else if(xraySheet.xrayResult == COMPOUND){
			Printf("Xray Technician [%d] records compound fracture on Adult Patient [%d]'s examination sheet.\n",id,xraySheet.patientID);
		}
		else {
			Printf("ERROR: The X-ray technician give the wrong result\n");
		}
		
		/*changed the sequence of printf and Signal*/
		Printf("X-ray Technician [%d] tells Patient [%d] to wait in Xray waiting room.\n",id,xraySheet.patientID);
		rpcSignalCV(xrayRoomLock[id],xrayPatientCV[id]);
		
		/*put the examsheet into the wall pocket and finish his task*/
		rpcGetExamSheet(xrayExamSheet[id],&myLocalExamSheet);
		rpcSetExamSheet(wallPocketXR[id],myLocalExamSheet);
		rpcSetStatus(wallPocketXR[id],USED);
		Printf("Xray Technician put the examsheet into the wall pocket\n");
		
		if(debugFlag == true) {
			Printf("myDEBUG:Xray put the sheet with name %s%d in WallPocetXR\n", myLocalExamSheet.name,myLocalExamSheet.patientID);
		}
		
		rpcWaitCV(xrayRoomLock[id],xrayPatientCV[id]);
		
	/*	rpcAcquireLock(patientLeftNumLock);	
		if(rpcGetValueMV(patientLeftNum,0) == rpcGetValueMV(patientNum,0)) {
			Exit(0);
		}
		rpcReleaseLock(patientLeftNumLock);	
	*/
	}
}